package com.qf.controller;

import com.qf.entity.Dept;
import com.qf.entity.Leave;
import com.qf.entity.LeaveType;
import com.qf.entity.User;
import com.qf.enums.OAExceptionEnum;
import com.qf.service.DeptService;
import com.qf.service.LeaveTypeService;
import com.qf.service.UserService;
import com.qf.utils.R;
import com.qf.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author 程云
 * @Date 2019/12/10 9:15
 * 人生在世 代码二字
 */
@Controller
@RequestMapping("/leave")
@Slf4j
public class LeaveController {

    /**
     * 请假controller
     */
    @Autowired
    private DeptService deptService;

    @Autowired
    private LeaveTypeService leaveTypeService;

    @Autowired
    private UserService userService;

    //转发到首页，用于做我自己的功能，可以删除
    @GetMapping("/index-ui")
    public String indexUI(){
        return "index";
    }

    /*
    * 转发到请请假页面*/
    @GetMapping("/leave-ui")
    public String leaveUI(Model model){
        //获取到当前登录用户的信息
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",user);
        //查询所有部门
        List<Dept> depts = deptService.findAllDept();
        model.addAttribute("dept",depts);
        //查询所有请假类型
        List<LeaveType> leaveTypes = leaveTypeService.findAllLeaveType();
        model.addAttribute("leaveTypes",leaveTypes);
        return "resource/renshi/demo1/list";
    }

    //查询部门经理
    @GetMapping("/two-check-m")
    @ResponseBody
    public ResultVO twoCheckM(){
        User user = userService.checkdeptmanager();
        if (user == null){
            log.info("【部门经理信息】 查询失败 user = {}",user);
            return R.error(OAExceptionEnum.INFO_NULL.getCode(), OAExceptionEnum.INFO_NULL.getMsg());
        }
        return R.ok(user);
    }

    //查询总监审批
    @GetMapping("/two-check-ci")
    @ResponseBody
    public ResultVO twoCheckCI(){
        User user = userService.checkdeptmanagerci();
        if (user == null){
            log.info("【总监信息】 查询失败 user = {}",user);
            return R.error(OAExceptionEnum.INFO_NULL.getCode(), OAExceptionEnum.INFO_NULL.getMsg());
        }
        return R.ok(user);
    }

    //查询总经理审批
    @GetMapping("/two-check-gm")
    @ResponseBody
    public ResultVO twoCheckGM(){
        User user = userService.checkdeptmanagergm();
        if (user == null){
            log.info("【总经理信息】 查询失败 user = {}",user);
            return R.error(OAExceptionEnum.INFO_NULL.getCode(), OAExceptionEnum.INFO_NULL.getMsg());
        }
        return R.ok(user);
    }

    //添加请假信息
    @PostMapping("/add-leave")
    @ResponseBody
    public ResultVO addLeave(@Valid Leave leave, BindingResult bindingResult){
        //任务id在service写，审核时间，数据库默认为空0:0:0:
        // 审核状态，service写默认初始化为0待审核
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【请假信息】 参数错误 msg = {},leave = {}",msg,leave);
            return R.error(OAExceptionEnum.INFO_PARAM_ERROR.getCode(),msg);
        }
        Integer count = leaveTypeService.addleave(leave);
        if (count != 1){
            log.info("【请假信息】 添加请假信息失败 count = {}",count);
            return R.error(OAExceptionEnum.LEAVE_ADD_ERROR.getCode(), OAExceptionEnum.LEAVE_ADD_ERROR.getMsg());        }
        return R.ok();
    }
}
